package zpin.dao;

import org.hibernate.Query;
import util.dao.HibernateDaoImpl;
import zpin.model.Authorization;
import zpin.model.Company;

public class CompanyDaoImpl extends HibernateDaoImpl<Company, Long> implements
		CompanyDao {
	public Company findByFk(Authorization authorization) {
		Query query = this.getSession().createQuery(
				"from " + Company.class.getName()
						+ " p where p.authorization.id="
						+ authorization.getId());
		if (query.uniqueResult() != null) {
			return (Company) query.list().get(0);
		} else {
			return null;
		}
	}

	@Override
	public boolean isUniqueName(String companyName) {
		// TODO Auto-generated method stub
		Query query = this.getSession().createQuery(
				"from " + Company.class.getName()
						+ " c where c.name=:companyName");
		query.setParameter("companyName", companyName);
		return query.uniqueResult() == null;
	}
}
